<template>
  <div class="form-layout">
    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor
        :itemList="cpItemList"
      />
    </div>
    <!--表单区域-->
    <div class="form-right" ref="formRight">
      <customForm :data="form" :rules="propRules" ref="editForm" :disabled="true">
        <template #content>
          <div id="box-base" class="box-title">采购信息</div>
          <customFormItem label="设备名称" name="deviceName" :parent="_self" :form="form"/>
          <customFormItem label="规格型号" name="deviceSpec" :parent="_self" :form="form" />
          <customFormItem label="单位" name="deviceUnit" :parent="_self" :form="form"
              :type="constCommon.formItemType.selectDict.name"
              :config="{dictType: constCommon.dictType.deviceUnit}"
          />
          <customFormItem label="数量" name="deviceQty" :parent="_self" :form="form"  :type="constCommon.formItemType.inputNumber.name"  @change="comAmount"  />
          <customFormItem label="进口设备" name="flagImport" :parent="_self" :form="form"
                          :type="constCommon.formItemType.switch.name"   />
          <customFormItem label="需要论证" name="flagArgument" :parent="_self" :form="form"
                          :type="constCommon.formItemType.switch.name" help="开启后该设备将进行采购论证"  />
          <customFormItem label="备注" name="remark" :parent="_self" :form="form"  />
        </template>
      </customForm>

        <!-- 论证-->
      <customForm :data="form" :rules="propRules" ref="editFormArgument" :disabled="true">
        <template #content>
          <div id="box-argument" class="box-title">论证结果</div>
          <customFormItem label="论证通过" name="flagArgumentPass" :parent="_self" :form="form"
                          :type="constCommon.formItemType.switch.name"/>
          <customFormItem label="论证说明" name="remarkArgument" :parent="_self" :form="form"
                          :type="constCommon.formItemType.textarea.name"
                          :config="{minRows: 1, autosize: true, maxRows: 5}"
                          fullWidth
          />
          <customFormItem
            label="附件"
            name="workFile"
            :parent="_self"
            :form="form"
            :type="constCommon.formItemType.slot.name"
            fullWidth
          >
            <template #content>
              <customUpload
                :data="{ fileRefId: form.id+ 'argument', fileRefTable: 'device_work_item', fileSavePath: 'device_work_item'}"
                :hide="propParent.cpFormNew"
                hideMsg="请先保存主单据后再上传"
                :max="20"
                :disabled="propReadonly || !cpIsPoArgument"
                multiple
              />
            </template>
          </customFormItem>
        </template>
      </customForm>

        <!-- 报价-->
      <customForm :data="form" :rules="propRules" ref="editForm" :disabled="propReadonly">
        <template #content>
          <div id="box-offer" class="box-title">报价中标信息</div>

          <data-view
            propViewKey="device_work_offer"
            ref="childDataViewOffer"
            :propFlowKey="propParent.propFlowKey"
            :propInitData="form"
            :propParentPage="_self"
            :propParentData="propParent.viewFormData"
            :propAutoFetchData="false"
            :propDisabledActiveNext="true"
            :propOnlyQueryAll="propReadonly"
            @customSelectFocus="customSelectFocus"
            :propHideFlowSelect="true"
            :propTableHide="propParent.cpFormNew"
            propTableHideMsg="请先保存主单据再操作"
          />

          <div id="box-file" class="box-title">相关附件</div>
          <customFormItem
            label="附件"
            name="workFile"
            :parent="_self"
            :form="form"
            :type="constCommon.formItemType.slot.name"
            fullWidth
          >
            <template #content>
              <customUpload
                :data="{ fileRefId: form.id+ 'offer', fileRefTable: 'device_work_item', fileSavePath: 'device_work_item'}"
                :hide="propParent.cpFormNew"
                hideMsg="请先保存主单据后再上传"
                :max="20"
                :disabled="propReadonly"
                multiple
              />
            </template>
          </customFormItem>
        </template>
      </customForm>
    </div>
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'
import customUpload from "@/components/custom/form/customUpload.vue";

export default {
  desc: '采购报价-报价明细',
  data() {
    return {
      form: {},
    }
  },
  computed: {
    // 是否是论证单
    cpItemList() {
      return [
        { name: 'box-base', title: '采购信息' },
        { name: 'box-argument', title: '论证结果' },
        { name: 'box-offer', title: '报价中标信息' },
        { name: 'box-file', title: '相关附件' },
      ]
    },
    // 是否是论证单
    cpIsPoArgument() {
      return this.propInitData.busType == this.constCommon.dictType.deviceBusTypePoArgument
    },
    // 是否是采购
    cpIsPo() {
      return this.propInitData.busType == this.constCommon.dictType.deviceBusTypePo
    },
  },
  created() {
    this.$emit('event-register', {
      //表单校验
      validate: (callback) => {
        return this.$refs.editForm.validate(callback)
      },

      //保存前操作
      beforeSave: () => {
        if (!this.form.id) {
          this.form.workId = this.propInitData.id
        }
        if (this.cpIsPo) {
          // 不需要论证的，直接论证通过
          if (this.form.flagArgument) {
            this.form.flagArgumentPass = false
          } else {
            this.form.flagArgumentPass = true
          }
        }

        this.$emit('data-callback', this.form)
        return true
      },

      //数据加载后
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
        console.log('this.propInitData', this.propInitData)
        this.loadDtl()
      },

      //数据保存后
      afterSave: () => {
      },
      beforeDelete: () => {
        return true
      },

      //数据删除后
      afterDelete: () => {
      },

      //流程提交前
      beforeFlow: (flowButtonKey) => {
        return true
      },

      //流程提交后
      afterFlow: (flowButtonKey) => {
      },

      //表单关闭前
      beforeClose: () => {
        return true
      }
    })
  },
  props: {
    //上级对象
    propParent: Object,
    //上级初始化数据
    propInitData: Object,
    //主键字段
    propPrimaryKey: String,
    //数据字典
    propDictionaries: Object,
    //当前操作行
    propCurrentRow: Object,
    //表单数据
    propViewFormData: Object,
    //是否只读
    propReadonly: Boolean,
    //操作状态
    propStatus: String,
    //表单规则
    propRules: Object
  },
  components: {
    customUpload,
    customAnchor
  },
  methods: {

    // 加载明细
    loadDtl() {
      let queryWrapper = this.$newWrapper()
      if (this.form.id) {
        queryWrapper.eq('workItemId', this.form.id)
      } else {
        queryWrapper.eq('workItemId', -1)
      }
      this.$refs.childDataViewOffer.setQueryWrapper(queryWrapper)
    },
    // 计算金额
    comAmount() {
      this.$set(this.form, 'deviceAmount', this.utilNum.toFixed(this.form.devicePrice *　this.form.deviceQty))
    },
    //供应商选择
    supplierSelect(row, refName) {
      // 当前选中行
      let currentRow = this.$refs.childDataViewOffer.getCurrentRow()
      console.log('currentRow', currentRow)
      console.log('row', row)
      this.$set(currentRow, 'supplierId', row.id)
      this.$set(currentRow, 'supplierName', row.name)
      this.$set(currentRow, 'workId', this.form.workId)
      this.$set(currentRow, 'workItemId', this.form.id)
      this.$set(currentRow, 'ledgerId', this.form.ledgerId)
    },
    //厂商选择
    mfgSelect(row, refName) {
      // 当前选中行
      let currentRow = this.$refs.childDataViewOffer.getCurrentRow()
      this.$set(currentRow, 'mfgId', row.id)
      this.$set(currentRow, 'mfgName', row.name)
    },
    // 自定义选择聚焦回调
    customSelectFocus(column, row) {
      // 供应商选择
      if (column.name == 'supplierName') {
        this.utilSelect.supplier(this.supplierSelect)
      }
      // 厂商选择
      if (column.name == 'mfgName') {
        this.utilSelect.mfg(this.mfgSelect)
      }
    }
  }
}
</script>
